package com.elsa.smart_buffet.test1;

import com.elsa.smart_buffet.SmartBuffetApplication;
import com.elsa.smart_buffet.mapper.*;
import com.elsa.smart_buffet.pojo.po.CategoryPrice;
import com.elsa.smart_buffet.pojo.po.CategorySum;
import com.elsa.smart_buffet.pojo.vo.CategoryPriceStats;
import com.elsa.smart_buffet.pojo.vo.ClassifyTableStats;
import com.elsa.smart_buffet.service.CategoryPriceService;
import com.elsa.smart_buffet.service.MenuService;
import com.elsa.smart_buffet.service.OrderService;
import com.elsa.smart_buffet.utils.CategoryUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : testOrder  //类名
 * @Author : elsa //作者
 */
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest()
@Slf4j
public class testOrder {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CategorySumMapper categorySumMapper;

    @Autowired
    private CategoryUtil categoryUtil;

    @Autowired
    private MenuService menuService;

    @Autowired
    private CalorieTableMapper calorieTableMapper;

    @Autowired
    private CategoryPriceMapper categoryPriceMapper;

    @Autowired
    private CategoryPriceService categoryPriceService;

    @Autowired
    private MenuIncomeExpenseMapper menuIncomeExpenseMapper;

    @Autowired
    private OrderService orderService;

    @Test
    public void testOrder() {

        log.info(String.valueOf(orderMapper.selectList(null)));
    }

    @Test
    public void testSelectAllOrderInOrderById() {

        log.info(String.valueOf(orderMapper.selectAllOrderInOrderById()));
    }

    @Test
    public void testSelectOrderInOrderByTime() {

        log.info(String.valueOf(orderMapper.selectOrderInOrderByTime("2023-02-01", "2023-03-18")));
    }

    @Test
    public void testSelectByOId() {

        log.info(String.valueOf(orderMapper.selectById(4)));
    }

    @Test
    public void selectOrderByConsumerName() {

        log.info(String.valueOf(orderMapper.selectOrderByConsumerName("Elsa1")));
    }

    @Test
    public void testGetCategorySumByTimeAndCategory() {


        int totalSUMCalorie = 0;
        List<CategorySum> listCategorySum = new ArrayList<>();
        for(int i = 1; i <= 4; i++) {
            CategorySum category = categorySumMapper.getCategorySumByTimeAndCategory("20230323", i + "");
            totalSUMCalorie += category.getSUMCalorie();
            listCategorySum.add(category);
            log.info(String.valueOf(category));
        }
        log.info(String.valueOf(totalSUMCalorie));
        List<ClassifyTableStats> list = new ArrayList<ClassifyTableStats>();
        for(int i = 1; i <= 4; i++) {
            list.add(new ClassifyTableStats(categoryUtil.getCategory(listCategorySum.get(i - 1).getCategory()), listCategorySum.get(i - 1).getSUMCalorie(), String.valueOf(Math.round((double) listCategorySum.get(i - 1).getSUMCalorie() / totalSUMCalorie * 10000) / 100) + '%'));
        }
        log.info(String.valueOf(list));
    }

    @Test
    public void testSelectClassifyTable() {
        menuService.selectClassifyTable("20230323");
    }


    @Test
    public void testCalorieTableMapper() {
        log.info(String.valueOf(calorieTableMapper.getCalorieTableByTime("20230323")));
    }

    @Test
    public void testGetCategoryPriceByTime() {
        log.info(String.valueOf(categoryPriceMapper.getCategoryPriceByTime("20230323")));
        List<CategoryPrice> list = categoryPriceMapper.getCategoryPriceByTime("20230323");
        List<CategoryPriceStats> categoryPriceStatsList = new ArrayList<>();
        int totalPrice = 0;
        int totalNum = 0;
        for (int i = 0; i < list.size(); i ++) {
            CategoryPriceStats categoryPriceStats = new CategoryPriceStats();
            categoryPriceStats.setCategory(list.get(i).getCategory());
            categoryPriceStats.setNum(list.get(i).getNum());
            totalNum += list.get(i).getNum();
            categoryPriceStats.setTotalPrice(list.get(i).getPrice());
            totalPrice += list.get(i).getPrice();
            categoryPriceStatsList.add(categoryPriceStats);
        }
        for (int i = 0; i < categoryPriceStatsList.size(); i ++) {
            categoryPriceStatsList.get(i).setRatio(String.valueOf(Math.round((double) categoryPriceStatsList.get(i).getNum() / totalNum * 10000) / 100) + '%');
            categoryPriceStatsList.get(i).setTotalPriceRatio(String.valueOf(Math.round((double) categoryPriceStatsList.get(i).getTotalPrice() / totalPrice * 10000) / 100) + '%');
        }
        log.info(String.valueOf(totalPrice));
        log.info(String.valueOf(totalNum));
        log.info(String.valueOf(categoryPriceStatsList));
    }

    @Test
    public void testGetCategoryService() {
        categoryPriceService.getCategoryPriceByTime("20230323");
    }

    @Test
    public void testMenuIncomeExpenseMapper() {
        log.info(String.valueOf(menuIncomeExpenseMapper.getMenuIncomeExpenseByTime("20230323")));
    }

    @Test
    public void selectOrderByCId() {
        log.info(String.valueOf(orderService.selectOrderByCId("4")));
    }

}
